package me.zhengjie.modules.quartz.task.snmp;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
public abstract class TrapMsgSender {

    public static PDU pdu;
    static {
        pdu = new PDU();
        pdu.setType(PDU.TRAP);
    }

    public abstract void handleTrapMsg();

    public void sendTrapMsg() {

        CommunityTarget target = new CommunityTarget(GenericAddress.parse("udp:127.0.0.1/" + 162),
                new OctetString("public"));
        target.setVersion(SnmpConstants.version2c);

        try {
            Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
            handleTrapMsg();
            log.info("======> pdu: {}", pdu);
            snmp.send(pdu, target);
        } catch (IOException e) {
            log.error("send trap msg fail {}", e);
        }
        log.info("send trap msg success.");
    }
}
